Blue Theme Orange Theme Green Theme Red Theme
 
ASP.NET Ecommerce Components
Home | Forums | Authors | Videos | Photos | Blogs | E-Books | Tips | Interviews
 | Consulting  
Submit Articles 
 Login
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
Technology Expand Technology
Website Expand Website
 Resources  
 Our Network  
Search :   Advanced Search »
Home » Visual Studio 2005 » How to read and write System Registry Keys using C# (VS.Net 2005)

How to read and write System Registry Keys using C# (VS.Net 2005)

Most of the time while working with the Enterprise Application will need to store the user preferences inside the windows system registry. In a general term Windows Registry is the database that holds all the settings Windows needs to function.

Author Rank:
Technologies: .NET 1.0/1.1,Visual C# .NET
Total downloads : 927
Total page views :  58503
Rating :
 4.8/5
This article has been rated :  5 times
  Add to Technorati Add to Technorati    Digg This Digg This    Add to del.icio.us Add to del.icio.us
    Rate this article Read/Post comments Support Us   Printable Version 

Download Files
ReadWriteRegistry.zip
Sponsored by

Looking for a book on Visual Studio 2005? Here is our book
Pro ASP.NET 2.0 E-Commerce in C# 2005
Price: US$ 30.00
Our Price: US$ 24.00

Description
Pro ASP.NET 2.0 E-Commerce in C# 2005 takes you through the e-commerce web application development cycle, from conception to coding to deployment and maintenance.
Browse more books here»

Normally, you don't interact directly with the registry; instead, you use some Windows utilities (such as those found in the Control Panel) or INI, SYS, BAT files etc. These tools allow you to customize the registry without actually working directly with it. There are, however, features that can only be set by editing the registry directly.

 

The hives

 

To get a better understanding of the inner workings of the registry, let us see the structure of the Registry.

 

The Registry has a hierarchal structure, like the directories on your hard disk. Each branch (denoted by a folder icon in the Registry Editor, see below) is called a Key. Each key can contain other keys, as well as Values. Each value contains the actual information stored in the Registry. There are three types of values; String, Binary, and DWORD - the use of these depends upon the context. 

 

 

 

 

 

 

 

 

 

 

 


 

 

The registry structure is as follows:

 

HKEY_CLASSES_ROOT

This key contains file extension associations. For instance, Windows can recognize a .doc file as a Microsoft Word document because of the settings in this key. Use the Folder Options command from the Tools menu in Windows Explorer instead of adding new extensions to this key.

 

HKEY_CURRENT_USER

This key holds profile information for the user that is currently logged on. Each time a user logs on; the user's profile is copied from the HKEY_USERS key to the HKEY_CURRENT_USER key. This key cannot be edited

 

HKEY_LOCAL_MACHINE

This key holds hardware and software information within its five subkeys, which are the following:  

  • Hardware - The Hardware subkey stores settings for device drivers, IRQ hooks, and so forth. It is re-created each time your computer boots.

  • Security accounts manager (SAM) - The SAM subkey stores information on security settings, user accounts and group memberships.

  • Security - The Security subkey holds information on local security policies such as password policy, user rights, account lockout and so forth.

  • Software - The Software subkey, which applies to all local users, stores data about installed software.

  • System - The System subkey stores information needed to boot Windows.

The first three (Hardware, SAM, and Security) cannot be modified.

 

HKEY_USERS

This key contains the default profile as well as profiles for all users who have logged on to the computer. This key can be edited, but exercise care when doing so.

 

HKEY_CURRENT_CONFIG

This key holds hardware information that is currently in use and allows for backwards compatibility with older applications and device drivers. The information stored here cannot be edited.

 

In this article you will see that how can we save user preferences in the System Registry.

Registry entry comprises to primary elements: Name and Value. Each entry is also stored in a collection of keys and subkeys. Any parent key may have one or more child keys, each with one or more names and values.

 

Fig 1.1 shows the snapshot of Registry Editor highlighting the User Application (Application developed by you and installed on your PC) setting.

 

 

Fig 1.1: Snapshot of Registry Editor

 

In this article you will get the good knowledge to manipulate the Registry settings, but don't do any changes with the other applications registry setting because it might affect the Operating system and you have to do the installation again.

 

Before starting the coding ......Follow the instruction according to the figure

 

Declare:-

 

string strWindowsState;

string strPath;

 

Read from Registry:-

RegistryKey regKeyAppRoot = Registry.CurrentUser.CreateSubKey(strPath); 

             strWindowsState = (string)regKeyAppRoot.GetValue("WindowState"); 

            if (strWindowsState != null && strWindowsState.CompareTo("Maximized") == 0)

                WindowState = System.Windows.Forms.FormWindowState.Maximized; 

            else if (strWindowsState != null && strWindowsState.CompareTo("Minimized") == 0)

                WindowState = System.Windows.Forms.FormWindowState.Minimized; 

            else

                WindowState = FormWindowState.Normal;

            label1.Text = strWindowsState;

            label3.Text = "";

            return;

 

Writing into Registry:-

 

strWindowsState = "";

            RegistryKey regKeyAppRoot = Registry.CurrentUser.CreateSubKey(strPath); 

            if (WindowState == FormWindowState.Maximized)

                strWindowsState = "Maximized"; 

            else if(WindowState == FormWindowState.Maximized)

                strWindowsState = "Minimized"; 

            else

                 strWindowsState = "Normal"; 

             regKeyAppRoot.SetValue("WindowState", strWindowsState);

             label3.Text = strWindowsState;

             label1.Text = "";

             return;

 

How to test:

After running the application change the window state of the form to minimized then click on write button.....it will write into Registry.... Then click on read button..... you can see that the state value is changing.

 

Output

 

Note:-The Hardware, SAM, and Security cannot be modified.



        Rate this article Read/Post comments Support Us   Printable Version
 About the author
Sushmita Kumari
 Post a new question or comment about this article
Subject:
Comment:
 [Top] Rate this article:-
Sponsors
Red Gate
ANTS Profiler™, Red Gate’s performance and memory profiling tool, has become industry standard for identifying performance bottlenecks in .NET applications. Whether you are developing in C#, VB.NET or managed C++, ANTS Profiler can pinpoint the exact lines of code responsible for performance inefficiencies. Optimizing your application is so much easier when you know exactly where to focus your performance boosting work!
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models. Click here to learn more about C# Consulting.
Latest Comments:
Subject Posted By Posted On
Re: Registry Sushmita 2/13/2006

Your question is not clear.Can u elaborate it ?

Reply | Email | Delete | Modify | 
Hellu. Nam 3/15/2006

- I intend creating an application that like Tweak UI or Registry Editor with C# ,can you introduce to me some class to write .? I learned basic C# and a litle WinForm. Thnks

Reply | Email | Delete | Modify | 
Rating nimesh 6/4/2007
Hi Sushmita, its excellent stuff...really good article..I am a recent Graduate and haven't worked on Registry before. Good stuff to start Nimesh
Reply | Email | Delete | Modify | 
Good job Mandeep 6/26/2008
Thank you Sush... simple yet elaborative for newbie Registry users... It'd have been better with comments... mandeep_sapkota@yahoo.com
Reply | Email | Delete | Modify | 
Similar Articles
How to read and write System Registry Keys using C# (VS.Net 2005) .
How to read Windows Registry? .
Accessing Registry using ASP.NET .
Click here to see more similar articles 
Support Us
Tell your friends about us
Is this article is a Bad Submission report us here
Submit articles, tutorials, and tips
Help by answering questions on our discussion forums
Help us correct errors and broken links
Looking for a job? Post your resume here
Looking for developers? Post your job here


 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.0.3072.29164
 © 1999 - 2008  Mindcracker LLC. All Rights Reserved